package com.maxsalad.uc.controller;

import com.maxsalad.common.message.MessageResult;
import com.maxsalad.uc.pojo.ResponseResult;
import com.maxsalad.uc.pojo.TPlatformShopEntity;
import com.maxsalad.uc.service.PlatformShopService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@RequestMapping(value = "/platformShop")
@RestController
public class PlatformShopController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(PlatformShopController.class);

    @Autowired
    private PlatformShopService platformShopService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseResult queryAllPlatformShop(@RequestParam Map paramMap) {
        try {
            TPlatformShopEntity platformShopEntity = new TPlatformShopEntity();
            platformShopEntity.setPlatformId(MapUtils.getLongValue(paramMap, "platformId", 0L));
            platformShopEntity.setShopId(MapUtils.getLongValue(paramMap, "shopId", 0L));


            Map map = platformShopService.queryAllPlatformShop(platformShopEntity);
            return getSucResultData(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseResult addPlatformShop(@RequestBody TPlatformShopEntity platformShopEntity) {
        try {
            MessageResult mr = platformShopService.saveOrUpdatePlatformShop(platformShopEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseResult updatePlatformShop(@PathVariable long id, @RequestBody TPlatformShopEntity platformShopEntity) {
        try {
            platformShopEntity.setId(id);
            MessageResult mr = platformShopService.saveOrUpdatePlatformShop(platformShopEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult deletePlatformShop(@PathVariable long id) {
        try {
            MessageResult mr = platformShopService.deletePlatform(id);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }


}